package com.boot.web;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

import java.security.Principal;

/**
 * @author Chunsheng.Zhang
 * @date 2021/4/16 12:59
 * @description : TODO
 */
@Controller
public class RouteController {



    @GetMapping("/index")
    public String index(ModelMap map){
        String username = null;


        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if (principal instanceof UserDetails) {

            username = ((UserDetails) principal).getUsername();

        }

        if (principal instanceof Principal) {

            username = ((Principal) principal).getName();

        }

        map.addAttribute("username",username);


        return "b";
    }



    @GetMapping("/toLogin")
    public String toLogin(){
        return "a";
    }

}
